/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.JedisUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuCashConfig;
import com.boxin.ims.modules.jiujiudevice.dao.JiuCashConfigDao;

/**
 * 押金配置Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuCashConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuCashConfigService.class);
	
	@Autowired
	private JiuCashConfigDao jiuCashConfigDao;
	
	public JiuCashConfig get(Long id) {
		return jiuCashConfigDao.findOne(id);
	}
	/**
	 * 获取押金全局配置
	 * @return
	 */
	public JiuCashConfig get() {
		JiuCashConfig conf = null;
		Object object = JedisUtils.getObject(JiuCashConfig.CASH_CONFIG_CACHE_KEY);
		if(null != object) {
			conf = (JiuCashConfig)object;
		}else {
			conf = jiuCashConfigDao.findOne(1L);
			JedisUtils.setObject(JiuCashConfig.CASH_CONFIG_CACHE_KEY, conf, 0);
		}
		return conf;
	}
	
	public Page<JiuCashConfig> find(Page<JiuCashConfig> page, JiuCashConfig jiuCashConfig) {
		DetachedCriteria dc = jiuCashConfigDao.createDetachedCriteria();
		if (jiuCashConfig.getId()!=null && jiuCashConfig.getId()>0){
			dc.add(Restrictions.eq("id", jiuCashConfig.getId()));
		}
		if (StringUtils.isNotEmpty(jiuCashConfig.getTitle())){
			dc.add(Restrictions.like("title", "%"+jiuCashConfig.getTitle()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return jiuCashConfigDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(JiuCashConfig jiuCashConfig) {
		jiuCashConfigDao.save(jiuCashConfig);
		JedisUtils.delObject(JiuCashConfig.CASH_CONFIG_CACHE_KEY);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuCashConfigDao.deleteById(id);
	}
	
}
